package cn.aaalei.shorturl.controller;

import cn.aaalei.shorturl.service.ApiService;
import cn.aaalei.shorturl.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @Value("${prefixUrl}")
    private String prefixUrl;

    @PostMapping("/short")
    public Result getShort(@RequestBody Map<String,String> requestMap) throws Exception{
        String url = requestMap.get("url");
        Result result = new Result();
        String shortUrl = apiService.getShort(url);
        shortUrl = prefixUrl + shortUrl;//+ ":" + port
        String qrCodeUrl = apiService.getQRCodeUrl(shortUrl);
        result.put("code",200);
        result.put("msg","success");
        result.put("shortUrl",shortUrl);
        result.put("qrCode",qrCodeUrl);
        return result;
    }

    @RequestMapping("/getFormer")
    public Result getFormer(@RequestParam("url") String url){

        Result result = new Result();
        return result;
    }
}
